#include "widget.h"
#include <QPainter>
#include <QTime>
#include <QTimer>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(200,200);
    QTimer *timer = new QTimer(this);
    timer->start(1000);
    connect(timer,&QTimer::timeout,this,QOverload<>::of(&Widget::update));
}

Widget::~Widget()
{
}

void Widget::paintEvent(QPaintEvent *event)
{
    static const QPoint hourHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -40)
    };

    static const QPoint minuteHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -70)
    };

    static const QPoint secHand[3] = {
        QPoint(2, 8),
        QPoint(-2, 8),
        QPoint(0, -90)
    };

    QColor hourColor(127, 0, 127);
    QColor minuteColor(0, 127, 127, 191);
    QColor secColor(127, 0, 127, 91);

    QTime time = QTime::currentTime();
    QPainter painter(this);
    painter.translate(width() / 2,height() / 2);

    //绘制时针
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(hourColor);
    painter.save();
    painter.rotate(30 * (time.hour() + time.minute() / 60.0));
    painter.drawPolygon(hourHand,3);
    painter.restore();

    //绘制分针
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(minuteColor);
    painter.save();
    painter.rotate(6 * (time.minute() + time.second() / 60.0));
    painter.drawPolygon(minuteHand,3);
    painter.restore();

    //绘制秒针
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(secColor);
    painter.save();
    painter.rotate(6 * time.second());
    painter.drawPolygon(secHand,3);
    painter.restore();


    //绘制表盘
    for(int i = 0; i < 60 ;i++)
    {
        if((i%5))
        {
            painter.setPen(minuteColor);
            painter.drawLine(92, 0, 96, 0);
        }
        else
        {
            painter.setPen(hourColor);
            painter.drawLine(88, 0, 96, 0);
        }
        painter.rotate(6);
    }
}

